package com.crape.account.controller;

import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.crape.account.bo.IAccountBO;
import com.crape.account.bo.IAccountHelperBO;
import com.crape.common.bean.LoginUser;
import com.crape.common.model.AccountInfo;
import com.crape.common.tools.DESEncoder;
import com.crape.common.tools.IDESEncoder;
import com.crape.common.utils.RegisterCheck;

@Controller
public class LoginController {

    private IAccountBO       accountBOImpl;
    private IAccountHelperBO accountHelperBOImpl;

    @Autowired
    public void setAccountBOImpl(IAccountBO accountBOImpl) {
        this.accountBOImpl = accountBOImpl;
    }

    @Autowired
    public void setAccountHelperBOImpl(IAccountHelperBO accountHelperBOImpl) {
        this.accountHelperBOImpl = accountHelperBOImpl;
    }

    @RequestMapping("/auth/login.jsp")
    public String login(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap, LoginUser loginUser) {
        if (loginUser == null) {
            modelMap.put("message", "用户信息为空！");
            return "/auth/login.html";
        }
        String loginId = loginUser.getLoginId();
        if (loginId == null) {
            modelMap.put("message", "登陆ID为空！");
            return "/auth/login.html";
        }
        AccountInfo params = new AccountInfo();
        boolean isEmail = RegisterCheck.isEmail(loginId);
        if (isEmail) {
            params.setEmail(loginId);
        } else {
            params.setUsername(loginId);
        }
        List<AccountInfo> acctlist = accountBOImpl.getAccountInfos(params);
        if (acctlist == null || acctlist.isEmpty()) {
            modelMap.put("message", "用户不存在！");
            return "/auth/login.html";
        }
        AccountInfo acct = acctlist.get(0);
        IDESEncoder encoder = DESEncoder.getInstance();
        String password = encoder.Encode(loginUser.getPassword().trim());
        if (!acct.getPassword().equals(password)) {
            modelMap.put("message", "密码错误！");
            return "/auth/login.html";
        }
        accountHelperBOImpl.setupAccountInfo(request, response, acct);
        return "redirect:/home.jsp";
    }

    @RequestMapping("/auth/logout.jsp")
    public String logout(HttpServletRequest request, HttpServletResponse response) {
        // 设置Session失效
        request.getSession().invalidate();
        // 设置Cookie失效
        Cookie loginIdCookie = new Cookie("loginIdCookie", null);
        Cookie passwordCookie = new Cookie("passwordCookie", null);
        response.addCookie(loginIdCookie);
        response.addCookie(passwordCookie);
        return "redirect:/auth/login.html";
    }
}
